﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Security.Cryptography.X509Certificates;

namespace XmlDSigEx
{
    public static class SignatureAlgorithmFactory
    {
        private static List<ISignatureProvider> _Providers = new List<ISignatureProvider>();
        
        static SignatureAlgorithmFactory()
        {
            // Default Configuration
            AddProvider(new RSAX509Provider());
            AddProvider(new ECDsaProvider());
        }

        public static void AddProvider(ISignatureProvider Provider)
        {
            lock (typeof(SignatureAlgorithmFactory))
            {
                _Providers.Insert(0, Provider);
            }
        }

        internal static ISignatureAlgorithm GetSignatureAlgorithm(XmlElement signature)
        {
            foreach (ISignatureProvider Provider in _Providers)
            {
                if (Provider.CanHandle(signature))
                    return Provider.GetSignatureAlgorithm(signature);
            }
            throw new UnknownAlgorithmException("No Provider could manage this signature.");
        }
    }
}
